﻿// ———————————————————————–
// <copyright file="VLList.cs" company="The MITRE Corporation">
//    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//    http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
// </copyright>
// ———————————————————————–
/////////////////////////////////////////////////////////////////////////////////////////////////
// VLList.cs - Library Class for a List of the ValueList Type; A Data Structure to Represent a Unique name and an associated list of values
// Project: IC.NET - EDXL Common Types Library
// Author:      Brian Wilkins MITRE Bedford
/////////////////////////////////////////////////////////////////////////////////////////////////

// Copyright (c) 2010 The MITRE Corporation. All rights reserved.
//
// NOTICE
// This software was produced for the U. S. Government
// under Contract No. FA8721-09-C-0001, and is
// subject to the Rights in Noncommercial Computer Software
// and Noncommercial Computer Software Documentation Clause
// (DFARS) 252.227-7014 (JUN 1995)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;

namespace EDXLSharp
{
  /// <summary>
  /// List wrapper for ValueList objects for a given root element
  /// </summary>
  [Serializable]
  public class VLList : List<ValueList>, IComposableMessage
  {
    #region Private Member Variables

    /// <summary>
    /// Name of the Root Element
    /// </summary>
    private string rootElementName;

    #endregion

    #region Constructors

    /// <summary>
    /// Initializes a new instance of the VLList class
    /// Default Ctor - Initializes List
    /// </summary>
    public VLList()
      : base()
    {
      this.rootElementName = String.Empty;
    }

    /// <summary>
    /// Initializes a new instance of the VLList class
    /// </summary>
    /// <param name="rootName">Root Element Name</param>
    public VLList(string rootName)
      : this()
    {
      this.rootElementName = rootName;
    }

    #endregion

    #region Public Accessors

    /// <summary>
    /// Gets or sets the RootElementName
    /// </summary>
    [XmlIgnore]
    public string RootElementName
    {
      get { return this.rootElementName; }
      set { this.rootElementName = value; }
    }

    /// <summary>
    /// Gets or sets
    /// A list of ValueLists
    /// </summary>
    [XmlIgnore]
    public List<ValueList> Values
    {
      get
      {
        return this.ToList<ValueList>();
      }

      set
      {
        this.Clear();
        this.AddRange(value);
      }
    }

    /// <summary>
    /// Gets or sets
    /// XML Serialization Object for Values
    /// </summary>
    public ValueList[] ValuesXML
    {
      get
      {
        if (this.Count == 0)
        {
          return null;
        }
        else
        {
          return this.ToArray();
        }
      }

      set
      {
        if (value != null)
        {
          this.Clear();
          this.AddRange(value);
        }
      }
    }

    #endregion

    #region Public Member Functions

    /// <summary>
    /// Method to determine if a given ValueList exists in the list
    /// </summary>
    /// <param name="listURN">ValueList urn to look for</param>
    /// <returns>Whether or not ValueList exists</returns>
    public bool Contains(string listURN)
    {
      ValueList temp = this.Find(delegate(ValueList vl) { return vl.ValueListURN == listURN; });

      return temp != null;
    }

    /// <summary>
    /// Method to return a given ValueList
    /// </summary>
    /// <param name="listURN">ValueList urn to look for</param>
    /// <returns>ValueList matching passed in urn</returns>
    public ValueList GetValueList(string listURN)
    {
      ValueList temp = this.Find(delegate(ValueList vl) { return vl.ValueListURN == listURN; });

      return temp;
    }

    /// <summary>
    /// Writes This IComposable Message To an Existing XML Document
    /// </summary>
    /// <param name="xwriter">Writer for Current XML Document - Since the Root Element of vl is Determined by Implementation this only writes VLURN and Values</param>
    public void WriteXML(XmlWriter xwriter)
    {
      try
      {
        this.Validate();
      }
      catch
      {
        return; ////empty list, so bail early
      }

      if (xwriter == (XmlWriter)null)
      {
        throw new ArgumentNullException("XWriter");
      }

      foreach (ValueList vl in this)
      {
        xwriter.WriteStartElement(this.rootElementName);
        vl.WriteXML(xwriter);
        xwriter.WriteEndElement();
      }

      xwriter.Flush();
    }

    /// <summary>
    /// Writes This IComposable Message To an Existing XML Document
    /// </summary>
    /// <param name="prefix">Defines the prefix to use when writing this object</param>
    /// <param name="xmlns">Defines the namespace to use when writing this object</param>
    /// <param name="xwriter">Writer for Current XML Document - Since the Root Element of vl is Determined by Implementation this only writes VLURN and Values</param>
    public void WriteXML(string prefix, string xmlns, XmlWriter xwriter)
    {
      try
      {
        this.Validate();
      }
      catch
      {
        return; ////either empty or null internal list, so bail early
      }

      if (xwriter == (XmlWriter)null)
      {
        throw new ArgumentNullException("XWriter");
      }

      foreach (ValueList vl in this)
      {
        xwriter.WriteStartElement(prefix, this.rootElementName, xmlns);
        vl.WriteXML(prefix, xmlns, xwriter);
        xwriter.WriteEndElement();
      }

      xwriter.Flush();
    }

    /// <summary>
    /// Reads This IComposable Message From an Existing XML Document
    /// </summary>
    /// <param name="rootnode">XMLNode That Points to the list of VLURN or Values...Not the Root ELement since that is standard specific</param>
    public void ReadXML(XmlNode rootnode)
    {
      if (rootnode == (XmlNode)null)
      {
        throw new ArgumentNullException("RootNode");
      }

      this.Clear();

      foreach (XmlNode node in rootnode.ChildNodes)
      {
        if (!String.IsNullOrEmpty(node.InnerText) && node.LocalName == this.rootElementName)
        {
          ValueList tmp = new ValueList();
          tmp.ReadXML(node);
          this.Add(tmp);
        }
      }
    }
    #endregion

    #region Protected Member Functions

    #endregion

    #region Private Member Functions

    /// <summary>
    /// Determines whether or not the data in this object is valid
    /// </summary>
    private void Validate()
    {
      if (this.Count < 1)
      {
        throw new Exception("ValueList list is empty.");
      }
    }

    #endregion
  }
}
